/**
 * store函数文件
 * created by xq on 2018/10/16
 */
/**
 * 创建store状态
 * @param {Function} reducer 
 * @returns {Object} 返回getState,dispatch,subscribe
 */
function createStore(reducer) {
  // store状态
  let state = null
  // 监听者数组
  const listeners = []
  // 获取state函数
  const getState = _ => state 
  // 订阅函数 插入监听函数到监听数组内
  const subscribe = listener => listeners.push(listener)
  // 状态修改包装
  const dispatch = (action) => {
    // 执行状态修改器
    state = reducer(state, action)
    // 循环监听数组，执行监听函数
    listeners.forEach(listener => listener())
  }
  dispatch({})
  return { getState, dispatch, subscribe }
}
export default createStore